package com.cqw.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * @Author: caoqingwen
 * @CreateTime: 2021-06-08 14:38
 * @Description: 多线程配置
 */
@Configuration
@EnableAsync       //开启异步，让多线程生效
public class AsyncConfiguration {


    @Bean("executorPool")
    public ExecutorService executorService(){
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        //核心线程数
        executor.setCorePoolSize(10);
        //最大线程数
        executor.setMaxPoolSize(20);
        //
        executor.setQueueCapacity(100);
        //存活时间
        executor.setKeepAliveSeconds(60);
        //线程名称前缀
        executor.setThreadNamePrefix("wg-thread");
        //拒绝策略
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.DiscardPolicy());
        //初始化
        executor.initialize();
        return executor.getThreadPoolExecutor();
    }

}
